package rafael.bot;

public class VectorUtils {

    public static int[] appendToHead(int[] hist, int move) {
        int[] newHist = new int[hist.length];
        for (int i = 1; i < hist.length; i++) {
            newHist[i] = hist[i - 1];
        }
        newHist[0] = move;
        return newHist;
    }

    public static double[] pDist(double[] state) {
        double min = Double.POSITIVE_INFINITY;
        double sum = 0;
        double[] u = state.clone();
        for (int i = 0; i < state.length; i++) {
            sum += state[i];
            if (state[i] < min) {
                min = state[i];
            }
        }
        sum -= min * state.length;
        if (sum == 0) {
            sum = 1;
        }
        for (int i = 0; i < state.length; i++) {
            u[i] = (state[i] - min) / sum;
        }
        return u;
    }

    public static double[] concat(double[] a, double[] b) {
        double[] c = new double[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static int[] concat(int[] a, int[] b) {
        int[] c = new int[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static double[] scalar(double a, double[] x) {
        double[] scalar = new double[x.length];
        for (int i = 0; i < x.length; i++) {
            scalar[i] = x[i] * a;
        }
        return scalar;
    }

    public static double[] plus(double[] w, double[] x) {
        double[] sum = new double[x.length];
        for (int i = 0; i < x.length; i++) {
            sum[i] = w[i] + x[i];
        }
        return sum;
    }

    public static double[] minus(double[] w, double[] x) {
        double[] sub = new double[x.length];
        for (int i = 0; i < x.length; i++) {
            sub[i] = w[i] - x[i];
        }
        return sub;
    }

    public static double dot(double[] x, double[] w) {
        double sum = 0;
        for (int i = 0; i < x.length; i++) {
            sum += x[i] * w[i];
        }
        return sum;
    }

    public static void printArray(double[] v) {
        for (int i = 0; i < v.length; i++) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(v[i]);
        }
        System.out.println();
    }
}
